package BNS.wheat;

import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import BNS.graphs.*;
import BNS.plumbing.*;

// TODO: BNSPersona should extend JComponent, and 
// startPanel should be deprecated in favour of start()
// and killPanel should be deprecated in favour of stop()

/** Implements the player GUI for the WheatMarketGame. */
public class WheatMktPersona extends BNSPersona {
		
	protected ClientLiaisonInterface game;
	protected Graph graphlet;
	protected JComponent panel;

	protected Market market;
	protected StatusPanel statusPanel;
	protected MarketPanel marketPanel;
	protected GraphPanel graphPanel;
	protected TradePanel tradePanel;
	
	protected int timeLimit, progressLimit;
	protected int numNodes;
	protected NodeState[] neighbourhood;
	public static final Color panelBackground= Color.lightGray;
	
	/** sole constructor. Retains parameters, but does not make anything visible; see startPanel().
		@param g the remote object (on the server) responsible for liaison between the client 
			and the BNSGame being played.
		@param gr the graphlet for this player
		@param nhbrs an array describing the state of the local market
		@param time the time limit on game length, in seconds
		@param progress the maximum value for social welfare, used to scale the display
		*/
	public WheatMktPersona(ClientLiaison g, Graph gr, NodeState[] nhbrs, int time, int progress) { 
		game=g; graphlet=gr; timeLimit=time; progressLimit=progress;
		neighbourhood= nhbrs;
		}
	
	public Node getEigenNode() { return graphlet.eigenNode; }
	
	public JComponent startPanel() {
		numNodes= graphlet.numNodes(); 
		panel= new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBackground(panelBackground);
		panel.setOpaque(true);
		
		statusPanel= new StatusPanel(timeLimit,progressLimit,true);
		panel.add("North", statusPanel);
		statusPanel.setMaximumSize(new Dimension(450,400));	// ANY limit seems to tame this
		
		graphPanel= new GraphPanel(ColorMap.identity());
//		panel.add("Center", graphPanel);				// take this out ?
		graphPanel.init(graphlet);
		
		tradePanel= new TradePanel(this);
		market= new Market(neighbourhood,null);
		marketPanel= new MarketPanel(graphlet, market);
		panel.add("Center", marketPanel);				// take this out ?
		panel.add("South", tradePanel);
		return panel;
		}
	
	public JComponent killPanel() { return panel; }
	
	synchronized public boolean order(Float price, Float volume) {
		try { if (!game.alter(new Market.Deal(price, volume))) {
			System.err.println("order refused"); return false; }
			else 
				System.err.println(graphlet.eigenNode+" order placed"); 
			}
		catch(RemoteException ex) { System.err.println(ex); }
		return true;
		}
	
	public int getCycleTime() { return 200; }
	
	synchronized public void gameUpdate(java.io.Serializable dataBlob) {
		if (!(dataBlob instanceof BNS.wheat.Envelope)) {
			System.err.println("got garbage blob "+dataBlob);
			return;
			}
		BNS.wheat.Envelope envelope= (BNS.wheat.Envelope)dataBlob;
		market.rebuild(envelope.local);
		marketPanel.rebuild(envelope.local);
		tradePanel.update(market);
		
		setMessage(envelope.status);
		timerUpdate(envelope.elapsedTime);
//		progressUpdate(envelope.currentProgress);
		statusPanel.setPay(envelope.payRate, envelope.cumulativePay);
		
/*		graphPanel.setNodeTips(tip);
		controlPanel.update(envelope.local.tFrac, envelope.local.kRate, envelope.local.pRate);
		graphPanel.repaint();
*/		}
	
	
	public void timerUpdate(long elapsedTime) { statusPanel.timeBar.setValue((int)elapsedTime);	}
	public void setMessage(String message) {
		if (message==null) return;
		statusPanel.messageBar.setText(message);
		Toolkit.getDefaultToolkit().beep();
		}
/*	public void progressUpdate(int progress) { 
		statusPanel.progressBar.setValue(progress); 
		String string= String.format("$%.2f/min",progress/1000.);
		statusPanel.progressBar.setString(string);
		}
*/		
	public String getTitle() { return "WheatMktPersona"; }
	
	} // EOC WheatMktPersona
